package com.situ.day1;

import org.junit.Test;

public class StringDemo {
    @Test
    public void test1() {
        //"helloabcnihao"字符串常量  ['a', 'b', 'c']   Abc
        String str1 = "abc";
        String str2 = new String("abc");
        String str3 = "abc";

        System.out.println(str1 == str2);//false
        System.out.println(str1 == str3);//true
        System.out.println(str2 == str3);//false

        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str3));
        System.out.println(str2.equals(str3));

    }

    @Test
    public void test2() {
        String str = "java AndroidA";
        System.out.println(str.length());//13
        char[] chars = str.toCharArray();
        char c = str.charAt(5);
        System.out.println(c);
        int index = str.indexOf('A');
        System.out.println(index);

        //parameter:参数
        //beginIndex – the beginning index, inclusive.   include
        //endIndex – the ending index, exclusive.        exclude
        // [beginIndex,endIndex)
        System.out.println(str.substring(5, 8));//And
        System.out.println("abc".isEmpty());
        //""空字符串也是一个对象
        System.out.println("".isEmpty());
        char ch = ' ';

        System.out.println("   Abc   ");
        System.out.println("   Abc   ".trim());
    }

    @Test
    public void test33() {
        //String str = "---java Android---";
        //String str = "java Android---";
        String str = "";
        //String str = "------";
        String newStr = trim(str);
        System.out.println(newStr);
    }

    private String trim(String str) {
        //卫操作
        if (str == null || str.length() == 0) {
            return "";
        }

        int startIndex = 0;
        int endIndex = str.length() - 1;

        while((startIndex <= endIndex) && (str.charAt(startIndex) == '-')) {
            startIndex++;
        }
        while((startIndex <= endIndex) && (str.charAt(endIndex) == '-')) {
            endIndex--;
        }

        return str.substring(startIndex, endIndex + 1);
    }

    @Test
    public void tes665() {
        String str1 = "Java";
        String str2 = "PHP";
        String str3 = "Python";
        String str4 = "UI";
        String str = str1 + str2 + str3 + str4;
        System.out.println(str);
    }

    @Test
    public void test12() {
        String str1 = "Java";
        String str2 = "PHP";
        String str3 = "Python";
        String str4 = "UI";
        StringBuffer buffer = new StringBuffer("C");
        /*buffer.append(str1);
        buffer.append(str2);
        buffer.append(str3);
        buffer.append(str4);
        System.out.println(buffer);*/
        buffer.append(str1).append(str2).append(str3).append(str4);
    }

}
